package com.example.myapplication;

import android.graphics.drawable.Drawable;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ExerciseFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ExerciseFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String CHINESE = "chinese";
    private static final String ENGLISH = "ENGLISH";
    private static final String ANSWER = "ANSWER";
    private static final String TIMES = "TIMES";
    private static final String RESULTS = "RESULTS";

    // TODO: Rename and change types of parameters
    private String chinese, answer;
    private ArrayList<String> english;
    private ArrayList<Integer> results;
    private int times;
    private TextView tv[], title, tv_answer, next;
    private LinearLayout container;

    public ExerciseFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param chinese Parameter 1.
     * @param english Parameter 2.
     * @return A new instance of fragment ExerciseFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static ExerciseFragment newInstance(String chinese, ArrayList<String> english, String answer, int times, ArrayList<Integer> results) {
        ExerciseFragment fragment = new ExerciseFragment();
        Bundle args = new Bundle();
        args.putString(CHINESE, chinese);
        args.putStringArrayList(ENGLISH, english);
        args.putString(ANSWER, answer);
        args.putInt(TIMES, times);
        args.putIntegerArrayList(RESULTS, results);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            chinese = getArguments().getString(CHINESE);
            english = getArguments().getStringArrayList(ENGLISH);
            answer = getArguments().getString(ANSWER);
            times = getArguments().getInt(TIMES);
            results = getArguments().getIntegerArrayList(RESULTS);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_exercise, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tv = new TextView[4];
        title = view.findViewById(R.id.tv_title);
        tv[0] = view.findViewById(R.id.tv1);
        tv[1] = view.findViewById(R.id.tv2);
        tv[2] = view.findViewById(R.id.tv3);
        tv[3] = view.findViewById(R.id.tv4);

        tv_answer = view.findViewById(R.id.tv_answer);
        tv_answer.setText(answer);

        next = view.findViewById(R.id.tv_next);


        container = view.findViewById(R.id.answer_container);

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                times ++;
                for (int i = 0; i < 4; i ++)
                    tv[i].setClickable(false);
                int lightGreen = 0xff99cc00, lightRed = 0xffff4444;
                ((TextView) v).setTextColor(lightGreen);
                ((TextView) v).setBackground(getContext().getResources().getDrawable(R.drawable.right_shape));

                boolean res = ((TextView) v).getText().equals(answer);
                if (res) {
                    results.add(1);
                } else {
                    results.add(0);
                }


                if (times == 4) {
                    next.setBackground(getContext().getResources().getDrawable(R.drawable.right_dark_solid));
                    next.setText("确认");
                    next.setOnClickListener(v1 -> {
                        getFragmentManager().beginTransaction().replace(R.id.ll_container, ResultFragment.newInstance(results)).commitAllowingStateLoss();
                    });
                } else {
                    Drawable draw_light, draw_dark;
                    if (res) {
                        draw_light = getContext().getResources().getDrawable(R.drawable.right_light_solid);
                        draw_dark = getContext().getResources().getDrawable(R.drawable.right_dark_solid);
                    }
                    else {
                        draw_light = getContext().getResources().getDrawable(R.drawable.wrong_light_solid);
                        draw_dark = getContext().getResources().getDrawable(R.drawable.wrong_dark_solid);
                    }

                    next.setBackground(draw_dark);
                    container.setBackground(draw_light);
                    next.setOnClickListener(view1 -> {
                        getFragmentManager().beginTransaction().replace(R.id.ll_container, ExerciseFragment.newInstance(chinese, english, answer, times, results)).commitAllowingStateLoss();
                    });

                    Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.animation_show_result);
                    container.startAnimation(animation);
                }
            }
        };

        title.setText(chinese);
        for (int i = 0; i < 4; i ++) {
            tv[i].setText(english.get(i));
            tv[i].setOnClickListener(listener);
        }
    }
}